<template>
    <div>
        <div class="container">
            <div class="form-box">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="旧密码" prop="oldpass">
                        <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpass">
                        <el-input type="password" v-model="ruleForm.newpass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>                    
                    <el-form-item>
                        <el-button type="primary" @click="saveEdit('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'editUserPass',
    data() {        
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.newpass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            //初始化组件变量
            ruleForm: {
                user_id:localStorage.getItem('user_id'),
                username:localStorage.getItem('ms_username'),
                oldpass:'',
                newpass: '',
                checkPass: '',
            },
            rules: {
                oldpass: [{ validator: validatePass, trigger: 'blur' }],
                newpass: [{ validator: validatePass, trigger: 'blur' }],
                checkPass: [{ validator: validatePass2, trigger: 'blur' }],
            }
        };
    },
    created() {},
    mounted() {},
    methods: {
        saveEdit(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {//验证通过执行提交，否则不执行
                    this.doEdit();
                } else {
                    this.$message.error("修改密码失败，请输入必要字段！");
                    return false;
                }
            });
        },
        async doEdit(){
            let res = await this.$http.post("editUserPass",this.ruleForm);
            if(res.state == 200){
                this.$message.success(res.message);
            }else{
                this.$message.error(res.message);
            }
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style>
</style>